var bindTel = new Vue({
	el: '.wxcontent',
	data:{
		invitecode:"",
		verifyButton:"发送验证码",
		disableVerify: false,
		data:{
			account:"",
			smsVerfiy:"",
		}
	},
	methods:{
		checkPhoneNumber: function () {
			if(0 === this.data.account.length ||
				(/^0?(1)[0-9]{10}$/.test(this.data.account)) === false){
				this.$message({
					showClose: false,
					message: "请输入正确的手机号码",
					type: 'error'
				});
				return false;
			}
			return true;
		},
		checkSmsVerify: function () {
			if(0 === this.data.smsVerfiy.length){
				this.$message({
					showClose: false,
					message: "请输入短信验证码",
					type: 'error'
				});
				return false;
			}
			return true;
		},
		getSms: function () { //获取短信验证码
			console.log("getSms ---- ");
			var reqData;
			if(true !== this.checkPhoneNumber()){
				return false;
			}
			reqData = {
				'phone':this.data.account
			};
			this.disableVerify = true;
			
			$.ajax({
				type: 'POST',
				url: "http://app.xianbing100.com/user/verification_code",
				dataType: 'json',
				data: JSON.stringify(reqData),
				xhrFields: {
			        	withCredentials: true // 携带跨域cookie
			    	},			
				contentType: 'application/json',
				success:function (data) {
					var resp = data;
					if(0 == resp['c']){
						var countdown=60;
						(function settime(val) {
							if (0 === countdown) {
								bindTel.disableVerify = false;
								bindTel.verifyButton = "获取验证码";
								countdown = 60;
							} else {
								setTimeout(function() {
									settime(val);
								},1000);
								countdown--;
								bindTel.verifyButton = countdown + "S";
							}
						})();
					}else{
						bindTel.$message({
							showClose: false,
							message: resp['i'],
							type: 'error'
						});
					}
				},
				error: function (xhr, error, exception) {
					console.warn(exception);
				}
			});
		},
		doSubmit: function () {//提交注册
			console.log("doSubmit ---- ");
			if(true !== this.checkPhoneNumber() || false === this.checkSmsVerify()){
				return false;
			}
			var reqData = {
				'loginKey':this.data.account,
				'password':this.data.smsVerfiy,
				'inviteId':this.invitecode
			};
			$.ajax({
				type: 'POST',
				url: "http://app.xianbing100.com/user/register_code",
				dataType: 'json',
				data: JSON.stringify(reqData),
				contentType: 'application/json',
				xhrFields: {
			        	withCredentials: true // 携带跨域cookie
			    	},			
				success:function (data) {
					var resp = data;
					if(0 == resp['c']){
						bindTel.data.account = '';
						bindTel.data.smsVerfiy = '';
						bindTel.$message({
							showClose: false,
							message: '领取成功',
							type: 'success'
						});
					}else{
						bindTel.$message({
							showClose: false,
							message: resp['i'],
							type: 'error'
						});
					}
				},
				error: function (xhr, error, exception) {
					console.warn(exception);
				}
			});
		}
		
	},
	beforeMount: function () {
		var _invitecode = getQueryString("code");
		if(_invitecode == "undefined"){
			this.invitecode = "";//带进来的邀请码
		}else{
			this.invitecode = getQueryString("code");//带进来的邀请码
		}
	},
});
